// pages/shop/order/payOrder/payOrder.js
const app = getApp();
const call = require('../../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderGroupId: '',
    paymentAmount: 0
  },
  onPay() {
    const url = `/service/app/v1/order/prePay?access_token=${app.globalData.access_token}`;
    const params = {
      "header": {},
      "data": {
        "orderId": this.data.orderGroupId,
        "payType": [34],
        "weixinOpenId": app.globalData.openId,
        "payChannel": 0,
        "paySubChannel": 1,
        "step": 0,
        "usePoints": 0
      }
    };
    console.log('地址:', url);
    console.log('传参：', JSON.stringify(params))
    call.postData(url, params,
      res => {
        console.log('调用预支付接口success', res);
        const {
          wxTimeStamp,
          wxNonceStr,
          wxPackage,
          wxSignType,
          wxPaySign
        } = res.data
        wx.requestPayment({
          timeStamp: wxTimeStamp,
          nonceStr: wxNonceStr,
          package: wxPackage,
          signType: wxSignType,
          paySign: wxPaySign,
          success(res) {
            console.log('支付成功',res);
            if (res.errMsg){
              wx.showToast({
                title: '支付成功',
              })
            }else{
              wx.showToast({
                title: res.msg,
                icon:'none'
              })
            }
            wx.navigateTo({
              url: '/pages/msg/msg_success'
            })
          },
          fail(err) {
            console.log('支付取消', err);
            wx.showToast({
              title: '支付取消',
              icon:'none'
            })
          }
        })
      },
      err => {
        console.log('调用预支付接口fail', err);
        wx.showToast({
          title: '服务器好像开小差了~',
          icon:'none',
          duration:5000
        })
      })
    // call.postData(url, params,
    //   res => {
    //     console.log('发起支付');
    //     var pay = res.data;
    //     var timeStamp = pay[0].timeStamp;
    //     var packages = pay[0].package;
    //     var paySign = pay[0].paySign;
    //     var nonceStr = pay[0].nonceStr;
    //     var param = {
    //       timeStamp: timeStamp,
    //       packages: packages,
    //       paySign: paySign,
    //       signType: "MD5",
    //       nonceStr: nonceStr
    //     }
    //     that._pay(param)
    //   },
    //   err => {
    //     console.log('生成订单失败')
    //   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      orderGroupId: options.orderGroupId,
      paymentAmount: options.paymentAmount
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})